package main

import (
	"context"
	"fmt"
	"time"

	"github.com/docker/docker/api/types"

	"github.com/docker/go-connections/nat"

	"github.com/docker/docker/api/types/container"

	"github.com/docker/docker/client"
)

func main() {
	client, err := client.NewClientWithOpts(client.WithHost("tcp://192.168.182.141:2345"))
	if err != nil {
		panic(err)
	}
	ctx := context.Background()
	container, err := client.ContainerCreate(
		ctx,
		&container.Config{
			Image: "mongo:4.4",
			ExposedPorts: nat.PortSet{
				"27017/tcp": {},
			},
		},
		&container.HostConfig{
			PortBindings: map[nat.Port][]nat.PortBinding{
				"27017/tcp": {{HostPort: "0"}},
			},
		},
		nil,

		nil,
		"",
	)
	if err != nil {
		panic(err)
	}

	err = client.ContainerStart(ctx, container.ID, types.ContainerStartOptions{})
	if err != nil {
		panic(err)
	}
	inspect, err := client.ContainerInspect(ctx, container.ID)
	if err != nil {
		panic(err)
	}
	fmt.Println(inspect.NetworkSettings.Ports["27017/tcp"][0].HostPort)
	time.Sleep(10 * time.Second)
	err = client.ContainerRemove(ctx, container.ID, types.ContainerRemoveOptions{Force: true})
	if err != nil {
		panic(err)
	}
}
